package licode.unisop.realm.application.executor;

import licode.unisop.provider.api.SiUserProvider;
import licode.unisop.realm.application.exception.UserErrorCode;
import licode.unisop.realm.application.gateway.SiRealmModelGateway;
import licode.unisop.realm.domainx.model.RealmModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 实现用户查询接口
 *
 * @author licode
 */
@Service
public class UserQueryUtil {
    @Resource
    private SiRealmModelGateway siRealmModelGateway;

    public SiUserProvider findUserPlugin(String realmId) {
        SiUserProvider provider;
        RealmModel realmModel = siRealmModelGateway.findRealmModel(realmId);

        if (null == realmModel || null == realmModel.getProvider()) {
            realmModel = siRealmModelGateway.findDefaultRealmModel();
        }

        provider = realmModel == null || realmModel.getProvider() == null ? null : realmModel.getProvider();

        if (null == provider) {
            throw UserErrorCode.USER_INVALID_PROVIDER.buildThrow();
        }

        return provider;
    }
}
